//
//  GETableView.m
//  ERP
//
//  Created by goldeney on 15/3/27.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GETableView.h"

@interface GETableView()

@property (nonatomic, strong) NSMutableDictionary *viewCellSaveDict;

@end


@implementation GETableView

- (void)dealloc
{
    [self.viewCellSaveDict removeAllObjects];
    self.viewCellSaveDict = nil;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.viewCellSaveDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.viewCellSaveDict = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)removeAllSaveCell
{
    if(self.viewCellSaveDict)
    [self.viewCellSaveDict removeAllObjects];
}

- (void)removeSaveCellInRow:(NSInteger)row inSection:(NSInteger)section
{
    NSString *key = [NSString stringWithFormat:@"%ld%ld",row,section];
    [self.viewCellSaveDict removeObjectForKey:key];
}
    
- (void)removeSaveCellKey:(NSString*)key
{
    if(key && key.length > 0)
    [self.viewCellSaveDict removeObjectForKey:key];
}

- (void)removeSaveCell:(id)viewCell
{
    if(!viewCell) return;
    for (id akey in [self.viewCellSaveDict allKeys]) {
        id singleCell= [self.viewCellSaveDict objectForKey:akey];
        if (singleCell == viewCell) [self.viewCellSaveDict removeObjectForKey:akey];
    }
}

#pragma mark -

- (id)getCellViewKey:(NSString*)key
{
    return [self.viewCellSaveDict objectForKey:key];
}

- (void)setCellView:(id)viewCell key:(NSString*)key
{
    if(viewCell) [self.viewCellSaveDict setObject:viewCell forKey:key];
}

#pragma mark -

- (id)getCellViewInRow:(NSInteger)row inSection:(NSInteger)section
{
    NSString *key = [NSString stringWithFormat:@"%ld%ld",row,section];
    return [self.viewCellSaveDict objectForKey:key];
}

- (void)setCellView:(id)viewCell inRow:(NSInteger)row inSection:(NSInteger)section
{
    NSString *key = [NSString stringWithFormat:@"%ld%ld",row,section];
    if(viewCell) [self.viewCellSaveDict setObject:viewCell forKey:key];
}

#pragma mark -

//获取显示节点数据
- (id)getCellClass:(Class)mClass pathForRow:(NSInteger)row inSection:(NSInteger)section
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    UITableViewCell *tableCell = [self cellForRowAtIndexPath:indexPath];
    for (id view in tableCell.contentView.subviews) {
        if ([view isKindOfClass:mClass]) {
            return view;
        }
    }
    return nil;
}

//获取保存节点数据
- (id)getCellSaveClass:(Class)mClass pathForRow:(NSInteger)row inSection:(NSInteger)section
{
    UITableViewCell *tableCell = [self getCellViewInRow:row inSection:section];
    for (id view in tableCell.contentView.subviews) {
        if ([view isKindOfClass:mClass]) {
            return view;
        }
    }
    return nil;
}

@end
